// mircoApp/ttlock/ttlock.js
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    keyword: "",
    lockPassworkShow: false,
    list: [],
    lock_list_show: false,
    lock_list: [],
    setLock_id: null,
    record_id: null,
    passwork: "",
    expire_time: "",

    isLoadMore: true,
    page: 1,
    total_page: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getData();
  },
  toStore(e){
    let { item } = e.currentTarget.dataset;
    let store_id = item.store.id;
    wx.navigateTo({
      url: '/packageC/o2o/HomeSeller/HomeSeller?store_id='+store_id,
    })
  },
  init() {
    this.setData({
      page: 1,
      total_page: 0,
      isLoadMore: true,
      list: [],
    });
  },
  search_method() {
    this.init();
    this.getData();
  },
  cancelBtn() {
    this.setData({
      lock_list_show: false,
      lock_list: [],
      setLock_id: null,
      record_id: null,
    });
  },
  confirmBtn() {
    if (!this.data.setLock_id) {
      app.tips("请选择锁");
      return;
    }
    this.getPassworkPost();
  },
  passworkClose() {
    this.setData({
      lockPassworkShow: false,
      record_id: null,
    });
    if (this.setLock_id) {
      this.init();
      this.getData();
      return;
    }
  },
  async getPassworkPost() {
    let json = { record_id: this.data.record_id };
    if (this.data.setLock_id) {
      json.lock_id = this.data.setLock_id;
    }
    let { data } = await app._postNetWork({
      url: app.getNetAddresss(
        "plugin.ttlock.frontend.controllers.lock.index.get-lock-password"
      ),
      data: json,
    });
    if (data.result) {
      this.setData({
        lock_list_show: false,
        lockPassworkShow: true,
        passwork: data.data.password,
        expire_time: data.data.expire_time,
      });
    } else {
      app.tips(data.msg);
    }
  },
  selectO(e) {
    let { item } = e.currentTarget.dataset;
    this.setData({
      setLock_id: item.id,
    });
  },
  async lockPassworkShow_Post(e) {
    let { item } = e.currentTarget.dataset;
    this.setData({
      record_id: item.id,
    });
    if (!item.lock) {
      this.setData({
        lock_list: item.lock_list,
        lock_list_show: true,
      });
      return;
    }
    this.getPassworkPost();
  },
  async getData() {
    let json = {
      search: {
        store_name: this.data.keyword,
      },
    };
    let { data } = await app._postNetWork({
      url: app.getNetAddresss("plugin.ttlock.frontend.controllers.lock.index"),
      data: json,
    });
    if (data.result) {
      let plugin_setting = wx.getStorageSync("yz_basic_info").plugin_setting;
      let plugin_name = plugin_setting.ttlock.plugin_name || "通通锁";
      wx.setNavigationBarTitle({
        title: plugin_name,
      });
      this.setData({
        list: data.data.data,
        total_page: data.data.last_page,
        isLoadMore: true,
      });
    } else {
      app.tips(data.msg);
    }
  },
  async getMoreData() {
    if (!this.data.isLoadMore) return;
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.setData({
        page: this.data.page + 1,
      });
      let json = {
        search: {
          store_name: this.data.keyword,
        },
        page: this.data.page,
      };
      let { data } = await app._postNetWork({
        url: app.getNetAddresss(
          "plugin.ttlock.frontend.controllers.lock.index"
        ),
        data: json,
      });
      if (data.result) {
        this.setData({
          list: this.data.list.concat(data.data.data),
          isLoadMore: true,
        });
      } else {
        app.tips(data.msg);
      }
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
